﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace MyLibellus
{
    public partial class MantenimentAuthor : Form
    {
        DataTable dtAuthors;
        SqlDataAdapter da;
        GestioAuthor ga;
        public MantenimentAuthor()
        {
            InitializeComponent();
            dtAuthors = DBAccess.getDataTAble("author");
        }

        private void MantenimentAuthor_Load(object sender, EventArgs e)
        {
            gridAuthors.DataSource = dtAuthors;
            SqlConnection con = DBAccess.getConnection();
            con.Open();
            da = DBAccess.getAdapter("select * from author", con, dtAuthors);
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
            ga = new GestioAuthor();

        }

        private void btnSortirAplicacio_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAfegirAuthor_Click(object sender, EventArgs e)
        {
            ga.A = null;
            ga.ShowDialog();
            if (ga.Aceptar)
            {

                //Modificar la fila.
                DataRow dr = dtAuthors.NewRow();
                dr["full_name"] = ga.A.FullName;

                dtAuthors.Rows.Add(dr);
                da.Update(dtAuthors);
                gridAuthors.Refresh();
            }
            
        }

        private void btnModificarAuthor_Click(object sender, EventArgs e)
        {
            //si la grid esta buida o no s'ha seleccionat cap fila no fa res.
            if (gridAuthors.SelectedRows.Count != 0 && gridAuthors.SelectedRows.Count != 0)
            {
                Author a = new Author();
                a.FullName = gridAuthors.SelectedRows[0].Cells[1].Value.ToString();
    
                ga.A = a;
                ga.ShowDialog();
                if (ga.Aceptar)
                {

                    //Modificar la fila.
                    DataRow dr = dtAuthors.Rows[gridAuthors.SelectedRows[0].Index];
                    dr["full_name"] = a.FullName;


                    da.Update(dtAuthors);

                }

            }
            gridAuthors.Refresh();
        }

        private void btnEliminarAuthor_Click(object sender, EventArgs e)
        {
            if (gridAuthors.SelectedRows.Count != 0)
            {
                if (MessageBox.Show("ESTA SEGUR D'ESBORRAR AQUEST AUTHOR?", "Confirmar Esborrar", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Author a = new Author();
                    a.Id = int.Parse(gridAuthors.SelectedRows[0].Cells[0].Value.ToString());
                    EliminarBookAuthor(a);
                    int pos = gridAuthors.SelectedRows[0].Index; //posicio del author dins la grid.
                    dtAuthors.Rows[pos].Delete();
                    da.Update(dtAuthors);
                    gridAuthors.Refresh();
                }
            }
        }

        public static void EliminarBookAuthor(Author a)
        {
            string del = "delete from bookAuthor where id_author='" + a.Id + "'";
            SqlConnection con = DBAccess.getConnection();
            con.Open();
            SqlCommand sc = new SqlCommand(del, con);
            sc.ExecuteNonQuery();
            con.Close();
        }
    }
}
